package leetcode.code0491;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {
	int mod = (int) 1e9 + 7;
	int base = 253;

	public List<List<Integer>> findSubsequences(int[] nums) {
		int len = nums.length;
		int size = (1 << len) - 1;
		List<List<Integer>> ans = new ArrayList<>();
		Set<Integer> exists = new HashSet<>();
		for (int i = 3; i <= size; i++) {
			int cur = i;
			int p = 0;
			int last = -101;
			int num = 0;
			List<Integer> list = new ArrayList<>();
			int hash = 0;
			boolean add = true;
			while (cur > 0) {
				if ((cur & 1) == 1) {
					if ((num = nums[p]) >= last) {
						hash = hash * base % mod + num + 101;
						hash %= mod;
						list.add(num);
						last = num;
					} else {
						add = false;
						break;
					}
				}
				cur >>= 1;
				p++;
			}
			if (add && list.size() >= 2 && !exists.contains(hash)) {
				ans.add(list);
				exists.add(hash);
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		List<List<Integer>> ans = HelpDebug.str2intlist1("[[1,3],[1,1],[1,1,2],[1,2]]");
		HelpDebug.compare(ans, this.findSubsequences(HelpDebug.str2array3("[1,3,1,2]")));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
